package com.imy.luascript.app.ui.main.fragment.project;

import android.util.Log;

import com.imy.common.constant.Constant;
import com.imy.common.util.ALog;
import com.imy.luascript.app.data.general.ProjectItem;
import com.imy.luascript.viewmodel.BaseImplView;
import com.imy.luascript.viewmodel.BaseListViewModel;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class ProjectViewModel extends BaseListViewModel {
    private String rootPath;

    public ProjectViewModel(BaseImplView implView) {
        super(implView);
    }

    public void loadData(String rootPath) {
        this.rootPath = rootPath;
        List<ProjectItem> list = new ArrayList<>();
        //判断该路径是不是顶级路径 不是的话 新增一个返回上一级的ITEM
        if (!rootPath.equals(Constant.PROJECT_PATH)){
            ProjectItem projectItem = new ProjectItem();
            projectItem.type=ProjectItem.TYPE_BACK;
            projectItem.file =new File(rootPath);
            list.add(projectItem);
        }
        File file = new File(rootPath);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            String absolutePath = file1.getAbsolutePath();
            ALog.i("文件:" + absolutePath);
            //过滤掉系统的文件夹
            if (!filterFile(absolutePath)) {
                ProjectItem projectItem = new ProjectItem();
                projectItem.type=ProjectItem.TYPE_FILE;
                projectItem.file = file1;
                list.add(projectItem);
            }
        }

        Collections.sort(list);
        listener.onFileGet(list);
    }

    private boolean filterFile(String path) {
        if (path.equals(Constant.ROOT_SCREEN_FILE_PATH)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void loadData() {
        loadData(rootPath);
    }

    public void setOnScanFileListener(OnScanFileListener onScanFileListener) {
        this.listener = onScanFileListener;
    }

    private OnScanFileListener listener;


    public interface OnScanFileListener {
        void onFileGet(List<ProjectItem> list);
    }
}
